package com.lsz.intelligentflowerwatering.viewmodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.espressif.provisioning.ESPConstants
import com.espressif.provisioning.ESPProvisionManager
import com.espressif.provisioning.WiFiAccessPoint
import com.espressif.provisioning.listeners.ProvisionListener
import com.espressif.provisioning.listeners.ResponseListener
import com.espressif.provisioning.listeners.WiFiScanListener
import java.lang.Exception
import java.util.ArrayList

sealed class DeviceConfigState {
    object WifiScanBegin : DeviceConfigState()
    object WifiScanEnd : DeviceConfigState()
    object WifiScanFail : DeviceConfigState()
    object WifiConfigSent : DeviceConfigState()
    object DeviceProvisioningSuccess : DeviceConfigState()
    object WifiConfigApplied : DeviceConfigState()
}

class WifiViewModel : ViewModel() {
    private var provisionManager = ESPProvisionManager.getInstance(null)
    var wifiDeviceList = mutableStateListOf<WiFiAccessPoint>()
    var deviceConfigState by mutableStateOf<DeviceConfigState>(DeviceConfigState.WifiScanEnd)
    val progress = mutableStateOf(0.0f)

    fun wifiScanStart() {
        wifiDeviceList.clear()
        deviceConfigState = DeviceConfigState.WifiScanBegin
        provisionManager.espDevice.scanNetworks(object : WiFiScanListener {
            override fun onWifiListReceived(wifiList: ArrayList<WiFiAccessPoint>) {
                wifiDeviceList.addAll(wifiList)
                deviceConfigState = DeviceConfigState.WifiScanEnd
            }

            override fun onWiFiScanFailed(e: Exception?) {
                deviceConfigState = DeviceConfigState.WifiScanFail
            }
        })
    }

    fun wifiConnect(ssid: String, pwd: String) {
        provisionManager.espDevice.sendDataToCustomEndPoint(
            "mqtt-data",
            "ouc10086".toByteArray(),
            object :
                ResponseListener {
                override fun onSuccess(returnData: ByteArray?) {
                    Log.i("sendDataToCustomEndPoint", "发送成功")
                    provisionManager.espDevice.provision(ssid, pwd, object : ProvisionListener {
                        override fun wifiConfigSent() {
                            deviceConfigState = DeviceConfigState.WifiConfigSent
                            Log.i("provision", "wifiConfigSent")
                            progress.value = 0.33f
                        }

                        override fun wifiConfigApplied() {
                            deviceConfigState = DeviceConfigState.WifiConfigApplied
                            Log.i("provision", "wifiConfigApplied")
                            progress.value = 0.66f
                        }

                        override fun deviceProvisioningSuccess() {
                            deviceConfigState = DeviceConfigState.DeviceProvisioningSuccess
                            Log.i("provision", "deviceProvisioningSuccess")
                            progress.value = 1.0f

                        }

                        override fun provisioningFailedFromDevice(failureReason: ESPConstants.ProvisionFailureReason?) {}
                        override fun wifiConfigFailed(e: Exception?) {}
                        override fun createSessionFailed(e: Exception?) {}
                        override fun wifiConfigApplyFailed(e: Exception?) {}
                        override fun onProvisioningFailed(e: Exception?) {}

                    })
                }

                override fun onFailure(e: Exception?) {
                }

            })
    }
}